package com.ruoyi.web.controller.pos;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.pos.domain.*;
import com.ruoyi.pos.service.IPosCheService;
import com.ruoyi.pos.service.IPosShangpinService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pos/pos_che")
public class PosCheController extends BaseController {

    @Autowired
    private IPosCheService posCheService;

    @Autowired
    private IPosShangpinService posShangpinService;

    @PostMapping("/addPosChe")
    public boolean addPosChe(@RequestBody PosChe posChe) {
        boolean flag = true;
        List<PosChe> posCheList = posCheService.selectPosCheList(posChe);
        if (posCheList != null && posCheList.size() > 0) {
            flag = false;
        } else {
            Integer mendianId = posCheService.selectMendianId(posChe.getShangpinId());
            posChe.setMendianId(mendianId);
            posChe.setGoodsCount(1);
            posCheService.insertPosChe(posChe);
        }
        return flag;
    }
    @GetMapping("/get_men_dian_list")
    public List<PosMendian> getMendianList(PosChe posChe){
        List<PosMendian> posMendianList = posCheService.selectPosMendianList(posChe);
        if(posMendianList != null && posMendianList.size() > 0){
            for(PosMendian pm : posMendianList){
                List<PosCheVO> posCheVOList = new ArrayList<>();
                posChe.setMendianId(pm.getId());
                List<PosChe> posCheList = posCheService.selectPosCheList(posChe);
                for(PosChe pc : posCheList){
                    PosShangpin posShangpin = posShangpinService.selectPosShangpinById(pc.getShangpinId());
                    PosColour posColour = posShangpinService.selectPosColourById(pc.getColourId());
                    PosSize posSize = posShangpinService.selectPosSizeById(pc.getSizeId());
                    PosCheVO posCheVO = new PosCheVO(posShangpin,posColour,posSize,pc);
                    posCheVOList.add(posCheVO);
                }
               pm.setPosCheVOList(posCheVOList);
            }
        }
        return posMendianList;
    }

    @GetMapping("/get_men_dian_list_and_kucun_check")
    public List<PosMendian> getMendianListAndKucunCheck(PosChe posChe){
        List<PosMendian> posMendianList = posCheService.selectPosMendianList(posChe);
        if(posMendianList != null && posMendianList.size() > 0){
            for(PosMendian pm : posMendianList){
                List<PosCheVO> posCheVOList = new ArrayList<>();
                posChe.setMendianId(pm.getId());
                List<PosChe> posCheList = posCheService.selectPosCheList(posChe);
                for(PosChe pc : posCheList){
                    PosShangpin posShangpin = posShangpinService.selectPosShangpinById(pc.getShangpinId());
                    PosColour posColour = posShangpinService.selectPosColourById(pc.getColourId());
                    PosSize posSize = posShangpinService.selectPosSizeById(pc.getSizeId());
                    PosCheVO posCheVO = new PosCheVO(posShangpin,posColour,posSize,pc);
                    posCheVOList.add(posCheVO);
                }
                pm.setPosCheVOList(posCheVOList);
            }
            for(PosMendian pm : posMendianList){
                for(PosCheVO pv : pm.getPosCheVOList()){
                    PosShangpinItem posShangpinItem = new PosShangpinItem();
                    posShangpinItem.setShangpinId(pv.getShangpinId());
                    posShangpinItem.setColourId(pv.getColourId());
                    posShangpinItem.setSizeId(pv.getSizeId());
                    List<PosShangpinItem> posShangpinItemList = posCheService.selectPosShangpinItemList(posShangpinItem);
                    Integer shangpinKucun = posShangpinItemList.get(0).getGoodsCount();
                    Integer xuanZhongShangpinCount = pv.getGoodsCount();
                    if(xuanZhongShangpinCount > shangpinKucun){
                        pv.setErrorCount("库存仅剩"+shangpinKucun+"个");
                        pv.setKucunError("false");
                    }
                }
            }
        }
        return posMendianList;
    }
    @GetMapping("/update_che")
    public int updatePosChe(PosChe posChe){
        return posCheService.updatePosChe(posChe);
    }

    @GetMapping("/pos_shangpin_count")
    public PosShangpinItem posShangpinCount(PosShangpinItem posShangpinItem){
        return posCheService.selectPosShangpinItemList(posShangpinItem).get(0);
    }
}
